package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import modelo_relacional.Inquilino;
import daoFramework.dbConn;

public class InquilinoDao {
	
	private dbConn connDB;

	public InquilinoDao() {
		connDB = new dbConn();
	}

	public void criaInquilino(Inquilino inquilino) {
		Connection conn = null;
		PreparedStatement stm = null;
		
		try {
			conn = connDB.getConnection();
			int index = 0;
			stm = conn.prepareStatement("insert into inquilino (Estado_Civil, Naturalidade, Nacionalidade, Nome, BI, Morada, NIF, Telefone, Localidade, Cod_Postal) "
					+ "values (?,?,?,?,?,?,?,?,?,?)");
			stm.setString(++index, inquilino.getEstado_civil());
			stm.setString(++index, inquilino.getNaturalidade());
			stm.setString(++index, inquilino.getNacionalidade());
			stm.setString(++index, inquilino.getNome());
			stm.setString(++index, inquilino.getBi());
			stm.setString(++index, inquilino.getMorada());
			stm.setString(++index, inquilino.getNif());
			stm.setString(++index, inquilino.getTelefone());
			stm.setString(++index, inquilino.getLocalidade());
			stm.setString(++index, inquilino.getCod_postal());
			stm.executeUpdate();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			connDB.closeAll(stm, conn);
		}
		
	}
	
	public void apagaInquilino(int inquilinoID){
		Connection conn = null;
		PreparedStatement stm = null;
		
		try {
			conn = connDB.getConnection();
			
			stm = conn.prepareStatement("delete from inquilino where inquilino_id = ?");
			stm.setInt(1, inquilinoID);
			stm.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			connDB.closeAll(stm, conn);
		}
	}
}
